package cn.edu.njust.epms.dao;

import cn.edu.njust.epms.common.R;
import cn.edu.njust.epms.entity.ATRecord;
import cn.edu.njust.epms.entity.IORecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface IODao {
        /**
         * 新增进出记录
         * @param record
         * @return
         */
        @Insert("insert into io_record (pid,rTime,io) values(#{pid},#{rTime},#{io})")
        public boolean Add(IORecord record);


        /**
         * 通用查询
         * @param r
         * @return
         */
        @Select("<script>"+
                "SELECT * FROM io_record inner join udetails on udetails.pid=io_record.pid"+
                "<where>"+
                "   <if test='r.name != null and r.name.length > 0'>"+
                "       AND name like #{r.name}"+
                "   </if>"+
                "   <if test='r.io != null'>"+
                "       AND io = #{r.io}"+
                "   </if>"+
                "   <if test='r.uId != null'>"+
                "       AND udetails.uId = #{r.uId}"+
                "   </if>"+
                "   <if test='r.pid != null and r.pid.length > 0'>"+
                "       AND udetails.pid like #{r.pid}"+
                "   </if>"+
                "   <if test='r.date1 != null and r.date2 != null'>"+
                "       AND rTime between #{r.date1} and #{r.date2}"+
                "   </if>"+
                "</where>"+
                "</script>")
        @Results({
                @Result(column = "aId",property = "aId"),
                @Result(column = "aTime",property = "aTime"),
                @Result(column = "io",property = "io"),
                @Result(column = "pid",property = "pid"),
                @Result(column = "pid",property = "ud.pid"),
                @Result(column = "name",property = "ud.name"),
                @Result(column = "addr",property = "ud.addr"),
                @Result(column = "phone",property = "ud.phone"),
                @Result(column = "gender",property = "ud.gender")
        })
        public List<IORecord> selectByCondition(@Param("r") R r);

        @Delete("delete from io_record where rId=#{rId}")
        public Boolean deleteIORecord(IORecord r);
    }